package com.tramy.rnsettings;

import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationManagerCompat;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;

import java.util.HashMap;
import java.util.Map;

@ReactModule(name = SettingsModule.NAME)
public class SettingsModule extends ReactContextBaseJavaModule {

    public static final String NAME = "ANDROIDSettings";

    public SettingsModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    @NonNull
    public String getName() {
        return NAME;
    }

    @Override
    public Map<String, Object> getConstants() {
        Map<String, Object> constants = new HashMap<>();
        constants.put("IMPORTANCE_NONE", Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? NotificationManager.IMPORTANCE_NONE : -1);
        constants.put("IMPORTANCE_MIN", Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? NotificationManager.IMPORTANCE_MIN : -1);
        constants.put("IMPORTANCE_LOW", Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? NotificationManager.IMPORTANCE_LOW : -1);
        constants.put("IMPORTANCE_DEFAULT", Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? NotificationManager.IMPORTANCE_DEFAULT : -1);
        constants.put("IMPORTANCE_HIGH", Build.VERSION.SDK_INT >= Build.VERSION_CODES.O ? NotificationManager.IMPORTANCE_HIGH : -1);
        constants.put("ADJUST_RESIZE", WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        constants.put("ADJUST_PAN", WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        return constants;
    }

    @ReactMethod
    public void createNotificationChannel(ReadableMap config) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(
                config.getString("id"),
                config.getString("name"),
                config.getInt("importance"));
            if (config.hasKey("vibrate")) {
                channel.enableVibration(config.getBoolean("vibrate"));
            }
            if (config.hasKey("lights")) {
                channel.enableLights(config.getBoolean("lights"));
            }
            if (config.hasKey("description")) {
                channel.setDescription(config.getString("description"));
            }

            NotificationManager manager = (NotificationManager) getReactApplicationContext()
                .getApplicationContext()
                .getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
        }
    }

    @ReactMethod
    public void isNotificationEnabled(Promise promise) {
        boolean isEnabled = NotificationManagerCompat
            .from(getReactApplicationContext().getApplicationContext())
            .areNotificationsEnabled();
        promise.resolve(isEnabled);
    }

    @ReactMethod
    public void setInputSoftMode(int mode, 
                                 Promise promise) {
        Activity activity = getCurrentActivity();
        if (activity != null) {
            activity.runOnUiThread(() -> {
                activity.getWindow().setSoftInputMode(mode);
                promise.resolve("");
            });
        }
    }

}
